package br.org.acessobrasil.facade;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.Database;

public class EntityManagerFactoryConfig {
	private EntityManagerFactoryConfig() {

	}

	public static EntityManagerFactory createEmFactoryMySql(DataSource dataSource) {
		Properties propertiesJpa = new Properties();
		propertiesJpa.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
		

		HibernateJpaVendorAdapter hibernateJpaVendorAdapterPro = new HibernateJpaVendorAdapter();
		hibernateJpaVendorAdapterPro.setDatabase(Database.MYSQL);
		hibernateJpaVendorAdapterPro.setShowSql(false);
		hibernateJpaVendorAdapterPro.setGenerateDdl(true);

		LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBeanPro = new LocalContainerEntityManagerFactoryBean();
		localContainerEntityManagerFactoryBeanPro.setDataSource(dataSource);
		localContainerEntityManagerFactoryBeanPro.setPersistenceUnitName("punit");
		localContainerEntityManagerFactoryBeanPro.setJpaVendorAdapter(hibernateJpaVendorAdapterPro);
		localContainerEntityManagerFactoryBeanPro.setJpaProperties(propertiesJpa);
		localContainerEntityManagerFactoryBeanPro.afterPropertiesSet();
		return localContainerEntityManagerFactoryBeanPro.getObject();
	}
}
